package com.dream.base.tools;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.dream.base.R;

/**
 * Creator : WangKH
 * Date : 2016/6/12.
 * Desc :
 */
public class DialogUtil {

    private TextView textView;

    private Context context;

    private int textRes;

    private Dialog loadingDialog;

    private int layoutLoading;

    private boolean isCanCancel = true;

    /**
     * @param context
     * @param layoutLoading
     */
    public DialogUtil(Context context, int layoutLoading) {
        this.context = context;
        this.layoutLoading = layoutLoading;

        if (layoutLoading == 0) {
            this.layoutLoading = R.layout.dialog_loading;
        }
    }

    public DialogUtil(Context context) {
        this.context = context;
        this.layoutLoading = R.layout.dialog_loading;
    }

    public void setCanCancel(boolean canCancel) {
        isCanCancel = canCancel;
    }

    public void setTextRes(int textRes) {
        this.textRes = textRes;
    }

    public Dialog getLoadingDialog() {


        return loadingDialog;
    }

    public void show() {

        if (loadingDialog == null && layoutLoading != 0) {

            loadingDialog = new Dialog(context, R.style.dialog);
            LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View reNameView = mLayoutInflater.inflate(layoutLoading, null);

            textView = (TextView) reNameView.findViewById(R.id.tv_loading_text);

            loadingDialog.setContentView(reNameView);
            loadingDialog.setCanceledOnTouchOutside(isCanCancel);
        }

        if (textRes == 0) {
            textView.setVisibility(View.GONE);
        } else {
            textView.setVisibility(View.VISIBLE);
            textView.setText(textRes);
        }

        loadingDialog.show();
    }

    public void dismiss() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }
}
